﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;

[System.Serializable]
public struct LaunchImage
{
    public Sprite sprite;
    public float showTime;
}

public class SplashPanel : MonoBehaviour {

    public Image splashImg;
    
    public LaunchImage[] launchImages;

    ZUIAnimTools m_UIAnim;
    ZUIAnimTools uiAnim
    {
        get
        {
            if (m_UIAnim == null)
                m_UIAnim = gameObject.AddComponent<ZUIAnimTools>();

            return m_UIAnim;
        }
    }

    private bool m_InAnim;
    private int m_CurIndex = -1;
    private float m_LastBeginShowTime;
    private bool m_ImageHasShowedEnd;
    
    Action m_AnimEnd;

    // Use this for initialization
    void Start ()
    {
        if (splashImg == null)
        {
            splashImg = GetComponentInChildren<Image>();
        }
        else
        {
            uiAnim.SetImgTxtAlpha(splashImg.rectTransform,0);
        }
    }

    private void OnDestroy()
    {
        m_AnimEnd = null;
    }

    public void Init(Action onEnd)
    {
        m_AnimEnd = onEnd;
        
        if (launchImages.Length == 0)
        {
            m_AnimEnd?.Invoke();
        }
        else
        {
            ShowLaunchImg(launchImages, 0);
        }
    }

    private const int BeginIndex = -1;
    
    void ShowLaunchImg(LaunchImage[] launchImages,int index)
    {
        if (launchImages == null || launchImages.Length ==0)
        {
            return;
        }

        m_InAnim = true;
        m_CurIndex = BeginIndex;
        
    }

    private ZTransformAnim m_ZTransformAnim;
    ZTransformAnim TransformAnim
    {
        get
        {
            if (m_ZTransformAnim == null)
                m_ZTransformAnim = gameObject.AddComponent<ZTransformAnim>();
            
            return m_ZTransformAnim;
        }
    }

    void OnImageBeginShow(LaunchImage launchImage)
    {
        GameDebug.LogShowy($"显示Image:{launchImage.sprite.name}   time：{Time.realtimeSinceStartup}");
        splashImg.sprite = launchImage.sprite;
        splashImg.SetNativeSize();
        uiAnim.UIAlpha(splashImg.rectTransform, 1, 0.2f);
        
        TransformAnim.SetCurve(AnimationCurve.Linear(0,0,1,1));
        TransformAnim.ScaleTo(splashImg.transform, Vector3.one * 1.2f, launchImage.showTime, null);
    }

    void OnImageEndShow(LaunchImage launchImage)
    {
        GameDebug.LogShowy($"隐藏Image:{launchImage.sprite.name}   time：{Time.realtimeSinceStartup}");
        uiAnim.UIAlpha(splashImg.rectTransform, 0, 0.2f, null);
    }

    // Update is called once per frame
	void Update ()
    {
        if (m_InAnim)
        {
            if (launchImages == null || m_CurIndex >= launchImages.Length)
            {
                try
                { 
                    if (m_AnimEnd != null)
                        m_AnimEnd();
                }
                catch (Exception e)
                {
                    GameDebug.LogError(e);
                }

                m_InAnim = false;
                return;
            }

            if (m_CurIndex == BeginIndex)
            {
                ///开始
                m_CurIndex = 0;

                LaunchImage firstImage = launchImages[m_CurIndex];
                OnImageBeginShow(firstImage);
                
                m_LastBeginShowTime = Time.realtimeSinceStartup;
                m_ImageHasShowedEnd = false;
            }
            
            LaunchImage launchImage = launchImages[m_CurIndex];

            float elapsedTime = Time.realtimeSinceStartup - m_LastBeginShowTime;
            if (elapsedTime > launchImage.showTime)
            {
               // GameDebug.LogShowy($"elapsedTime:{elapsedTime}  launchImage:{launchImage.sprite.name} time：{launchImage.showTime}");

                if (elapsedTime > launchImage.showTime + 0.2f) 
                {   
                    m_CurIndex++;
                    
                    if (m_CurIndex >= launchImages.Length)
                    {
                        if (m_AnimEnd != null)
                            m_AnimEnd();
                
                        m_InAnim = false;
                        return;
                    }

                    LaunchImage nextImage = launchImages[m_CurIndex];
                    OnImageBeginShow(nextImage);
                    
                    m_LastBeginShowTime = Time.realtimeSinceStartup;
                    m_ImageHasShowedEnd = false;
                }
                else
                {
                    if (!m_ImageHasShowedEnd)
                    {
                        m_ImageHasShowedEnd = true;
                        
                        OnImageEndShow(launchImage);
                    }
                }
            }
        }
	}
}
